//
//  YZLSeachViewController.m
//  Car
//
//  Created by dllo on 15/11/5.
//  Copyright © 2015年 岳明. All rights reserved.
//

#import "YZLSeachViewController.h"
#import "Macro.h"
#import "YZLSearchButtonView.h"
#import "HTTPTOOL.h"

#import "YZLSearchAllView.h"
#import "YZLSearchNewsView.h"
#import "YZLSearchPostView.h"
#import "YZLSearchCarView.h"

#import "YZLCarDetailViewController.h"
#import "YZLSearchModel.h"
#import "YZLSubBrandListModel.h"
#import "YZLCarImageViewController.h"
#import "YZLSeriesListModel.h"
#import "YZLImageListModel.h"
#import "YZLNewsLinkViewController.h"
#import "YZLNewsModel.h"
#import "YZLNewsListModel.h"
#import "TieDeatailViewController.h"
#import "YZLPostModel.h"
#import "YZLDiscuss.h"
#import "YZLCarListModel.h"
#import "YZLCarTypeMessageViewController.h"

@interface YZLSeachViewController ()<UISearchResultsUpdating, UISearchBarDelegate, YZLSearchButtonViewDelegate, YZLSearchAllViewDelegate, YZLSearchNewsViewDelegate, YZLSearchPostViewDelegate, YZLSearchCarViewDelegate>

@property (nonatomic, strong) UISearchController *search; /**< 搜索 */
@property (nonatomic, strong) YZLSearchButtonView *searchButton; /**< 页面切换 */
@property (nonatomic, assign) NSInteger whileButton; /**< 确定点击的是哪一个 button */
@property (nonatomic, strong) NSString *transcodingString; /**< 输入框中内容转码后的字符串 */

@property (nonatomic, strong) YZLSearchAllView *searchAllView;
@property (nonatomic, strong) YZLSearchNewsView *searchNewsView;
@property (nonatomic, strong) YZLSearchPostView *searchPostView;
@property (nonatomic, strong) YZLSearchCarView *searchCarView;
@property (nonatomic, assign) NSInteger buttonIndex;

@property (nonatomic, strong) UIImageView *helloImageView; /**< 好车搜一搜 */

@end

@implementation YZLSeachViewController

- (void)viewDidAppear:(BOOL)animated {
    //    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = YES;
}

/*在viewWillDisappear中要将UISearchController移除, 否则切换到下一个View中, 搜索框仍然会有短暂的存在 */
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.search.searchBar.hidden = NO;
    if (self.search.active) {
        self.search.active = NO;
        [self.search.searchBar removeFromSuperview];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor colorWithRed:248 / 255.f green:248 / 255.f blue:248 / 255.f alpha:1];
    
    [self createSearchBar];
    
    self.helloImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"search_Hello"]];
    [self.view addSubview:self.helloImageView];
    self.helloImageView.frame = CGRectMake(0, 117, kWidth, kHeight - 117);
    
}

#pragma mark - 创建搜索
- (void)createSearchBar {
    
    UIView *searchView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, kWidth, 40)];
    [self.view addSubview:searchView];
    // 创建搜索条，将自身设置为展示结果的控制器
    self.search = [[UISearchController alloc] initWithSearchResultsController:nil];
    // 设置渲染颜色
    self.search.searchBar.tintColor = [UIColor blueColor];
    // 设置状态调颜色
    self.search.searchBar.barTintColor = [UIColor colorWithRed:248 / 255.f green:248 / 255.f blue:248 / 255.f alpha:1];
    // 设置开始搜索时背景显示与否
    self.search.dimsBackgroundDuringPresentation = NO;
    // 适应整个屏幕
    [self.search.searchBar sizeToFit];
    // 设置显示搜索结果的控制器
    self.search.searchResultsUpdater = self;
    // 修改搜索框内的提示内容
    self.search.searchBar.placeholder = @"请输入搜索的内容";
    self.search.searchBar.delegate = self;
    [searchView addSubview:self.search.searchBar];
    [self.search.searchBar setShowsCancelButton:YES];
}

#pragma mark - 搜索协议
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
    // 取出搜索输入框中的文字
    
    NSString *searchString = [NSString string];
    
    if (![searchString isEqualToString:searchController.searchBar.text]) {
        [self.searchAllView removeFromSuperview];
        [self.searchNewsView removeFromSuperview];
        [self.searchPostView removeFromSuperview];
        [self.searchCarView removeFromSuperview];
        searchString = searchController.searchBar.text;
        
        if (![searchString isEqualToString:@""]) {
            [self createSearchCarViewWithString:searchString];
            [self createSearchPostViewWithString:searchString];
            [self createSearchNewsViewWithString:searchString];
            [self createSearchAllViewWithString:searchString];
            
            switch (self.buttonIndex) {
                case 0:
                    [self.view bringSubviewToFront:self.searchAllView];
                    break;
                case 1:
                    [self.view bringSubviewToFront:self.searchNewsView];
                    break;
                case 2:
                    [self.view bringSubviewToFront:self.searchCarView];
                    break;
                case 3:
                    [self.view bringSubviewToFront:self.searchPostView];
                    break;
                    
                default:
                    break;
            }
        }
    }
    
    [self createSearchButtonView];
}

#pragma mark - 综合
- (void)createSearchAllViewWithString:(NSString *)searchString {
    self.searchAllView = [[YZLSearchAllView alloc] initWithFrame:CGRectMake(0, 117, kWidth, kHeight - 117)];
    [self.view addSubview:self.searchAllView];
    self.searchAllView.bodyString = searchString;
    self.searchAllView.backgroundColor = [UIColor grayColor];
    self.searchAllView.delegate = self;
    
}

#pragma mark - tableViewHeader 点击事件协议
- (void)tableViewHeaderPuseNextViewControllerWithString:(NSString *)seriesId {
    YZLCarDetailViewController *carDetailVC = [[YZLCarDetailViewController alloc] init];
    carDetailVC.seriesId = seriesId;
    [self.navigationController pushViewController:carDetailVC animated:YES];
}

#pragma mark - tableViewCell 点击事件协议
- (void)pushNextViewOfSearchAllsWithIndex:(NSIndexPath *)indexPath WithModel:(YZLSearchModel *)searchModel withKayArray:(NSArray *)kayArray {
    if ([searchModel.resultType integerValue] == 1 && indexPath.section == 0) {
        YZLSeriesListModel *seriesModel = [searchModel.seriesList objectAtIndex:indexPath.row];
        YZLCarImageViewController *carImageVC = [[YZLCarImageViewController alloc] init];
        carImageVC.itemNum = 0;
        carImageVC.price = seriesModel.guidePrice;
        carImageVC.dropPrice = seriesModel.dropPrice;
        NSMutableArray *imageArray = [NSMutableArray array];
        for (YZLImageListModel *model in seriesModel.imageList) {
            NSDictionary *dic = [NSDictionary dictionaryWithObject:model.bigImageUrl forKey:@"imageBig"];
            [imageArray addObject:dic];
        }
        carImageVC.imageArr = imageArray;
        [self.navigationController pushViewController:carImageVC animated:YES];
    } else if ([searchModel.resultType integerValue] == 2 && indexPath.section < searchModel.subBrandList.count) {
        YZLSubBrandListModel *subModel = [searchModel.subBrandList objectAtIndex:indexPath.section];
        YZLSeriesListModel *serModel = [subModel.seriesList objectAtIndex:indexPath.row];
        YZLCarDetailViewController *carDetailVC = [[YZLCarDetailViewController alloc] init];
        carDetailVC.seriesId = serModel.seriesId;
        [self.navigationController pushViewController:carDetailVC animated:YES];
    } else if (indexPath.section == kayArray.count - 2) {
        YZLNewsLinkViewController *newsVC = [[YZLNewsLinkViewController alloc] init];
        YZLNewsModel *newsModel = [searchModel.news objectForKey:@"news"];
        YZLNewsListModel *newsListModel = [newsModel.newsList objectAtIndex:indexPath.row];
        newsVC.urlString = newsListModel.newsLink;
        newsVC.typeString = @"2";
        [self.navigationController pushViewController:newsVC animated:YES];
    } else {
        YZLPostModel *postModel = [searchModel.post objectForKey:@"post"];
        YZLDiscuss *discussModel = [postModel.postList objectAtIndex:indexPath.row];
        TieDeatailViewController *tieDeatailVC = [[TieDeatailViewController alloc] init];
        tieDeatailVC.discuss = discussModel;
        [self.navigationController pushViewController:tieDeatailVC animated:YES];
    }
    
}

#pragma mark - 文章
- (void)createSearchNewsViewWithString:(NSString *)searchString {
    self.searchNewsView = [[YZLSearchNewsView alloc] initWithFrame:CGRectMake(0, 117, kWidth, kHeight - 117)];
    [self.view addSubview:self.searchNewsView];
    self.searchNewsView.bodyString = searchString;
    self.searchNewsView.delegate = self;
}

- (void)pushNextViewOfSearchNewsWithIndex:(NSIndexPath *)indexPath withModel:(YZLNewsModel *)newsModel {
    YZLNewsLinkViewController *newsVC = [[YZLNewsLinkViewController alloc] init];
    YZLNewsListModel *newsListModel = [newsModel.newsList objectAtIndex:indexPath.row];
    newsVC.urlString = newsListModel.newsLink;
    newsVC.typeString = @"2";
    [self.navigationController pushViewController:newsVC animated:YES];
}

#pragma mark - 贴吧
- (void)createSearchPostViewWithString:(NSString *)searchString {
    self.searchPostView = [[YZLSearchPostView alloc] initWithFrame:CGRectMake(0, 117, kWidth, kHeight - 117)];
    [self.view addSubview:self.searchPostView];
    self.searchPostView.searchString = searchString;
    self.searchPostView.delegate = self;
}

- (void)pushNextViewOfSearchPostWithIndex:(NSIndexPath *)indexPath withModel:(YZLPostModel *)postModel {
    YZLDiscuss *discussModel = [postModel.postList objectAtIndex:indexPath.row];
    TieDeatailViewController *tieDeatailVC = [[TieDeatailViewController alloc] init];
    tieDeatailVC.discuss = discussModel;
    [self.navigationController pushViewController:tieDeatailVC animated:YES];
}

#pragma mark - 车系
- (void)createSearchCarViewWithString:(NSString *)searchString {
    self.searchCarView = [[YZLSearchCarView alloc] initWithFrame:CGRectMake(0, 117, kWidth, kHeight - 117)];
    [self.view addSubview:self.searchCarView];
    self.searchCarView.bodyString = searchString;
    self.searchCarView.delegate = self;
}

- (void)pushNextViewOfSearchCarWithIndexPath:(NSIndexPath *)indexPath withModel:(YZLSearchModel *)searchModel {
    
    if ([searchModel.resultType integerValue] == 1) {
        if (indexPath.row == 0 ) {
            YZLCarDetailViewController *carDetailVC = [[YZLCarDetailViewController alloc] init];
            YZLSeriesListModel *serModel = [searchModel.seriesList objectAtIndex:indexPath.section];
            carDetailVC.seriesId = serModel.seriesId;
            [self.navigationController pushViewController:carDetailVC animated:YES];
        } else {
            YZLCarTypeMessageViewController *carTypeMessageVC = [[YZLCarTypeMessageViewController alloc] init];
            YZLSeriesListModel *seriesModel = [searchModel.seriesList objectAtIndex:indexPath.section];
            YZLCarListModel *carModel = [seriesModel.carList objectAtIndex:indexPath.row];
            carTypeMessageVC.carId = carModel.carId;
            [self.navigationController pushViewController:carTypeMessageVC animated:YES];
        }
    } else if ([searchModel.resultType integerValue] == 2 ) {
        YZLCarDetailViewController *carDetailVC = [[YZLCarDetailViewController alloc] init];
        YZLSubBrandListModel *subModel = [searchModel.subBrandList objectAtIndex:indexPath.section];
        YZLSeriesListModel *serModel = [subModel.seriesList objectAtIndex:indexPath.row];
        
        carDetailVC.seriesId = serModel.seriesId;
        [self.navigationController pushViewController:carDetailVC animated:YES];
    }
}

#pragma mark - 点击取消撤销页面
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

- (void)createSearchButtonView {
    if (!self.searchButton) {
        self.searchButton = [[YZLSearchButtonView alloc] initWithFrame:CGRectMake(0, 65, kWidth, 50)];
        [self.view addSubview:self.searchButton];
        self.searchButton.delegate = self;
    }
}

- (void)switchTheViewWithButton:(UISegmentedControl *)button {
    NSInteger index = button.selectedSegmentIndex;
    switch (index) {
        case 0:
            //            [self createSearchAllViewWithString:self.search.searchBar.text];
            [self.view bringSubviewToFront:self.searchAllView];
            self.buttonIndex = 0;
            break;
        case 1:
            //            [self.view sendSubviewToBack:self.searchAllView];
            [self.view bringSubviewToFront:self.searchNewsView];
            self.buttonIndex = 1;
            break;
        case 2:
            
            [self.view bringSubviewToFront:self.searchCarView];
            self.buttonIndex = 2;
            break;
        case 3:
            
            [self.view bringSubviewToFront:self.searchPostView];
            self.buttonIndex = 3;
            break;
            
        default:
            break;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
